﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace ExcelAutomator.Configuration
{
	[Serializable]
	[XmlRoot("executionSet", Namespace = Constants.Namespace)]
	public class ExecutionSetConfiguration : IXmlSerializable
	{
		public ExecutionSetConfiguration()
		{
			Executors = new List<ExecutorConfigurationWrapper>();
		}

		public string Id { get; set; }

		public List<ExecutorConfigurationWrapper> Executors { get; set; }

		public XmlSchema GetSchema()
		{
			throw new NotImplementedException();
		}

		public void ReadXml(XmlReader reader)
		{
			var xDoc = XDocument.Parse(reader.ReadOuterXml());

			foreach (var xElement in xDoc.Elements())
			{
				if (xElement.Name.LocalName.Equals("executionSet"))
				{
					string id;
					if (xElement.TryGetAttributeValue("id", string.Empty, out id))
					{
						Id = id;
					}
				}

				foreach (var xExecutor in xElement.Descendants())
				{
					Executors.Add(new ExecutorConfigurationWrapper(xExecutor.Name.LocalName, xExecutor.Name.Namespace.NamespaceName, xExecutor.ToString()));
				}
               
			}
		}

		public void WriteXml(XmlWriter writer)
		{
			throw new NotImplementedException();
		}
	}
}
